package com.mxl.common;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;



/**
 * @author mxl
 * @version 1.0
 */
public class HttpClientUtil {

	private static final int READ_TIMEOUT = 10000;// 10s
	private static final int CONNECTION_TIMEOUT = 10000; // 10s

	@SuppressWarnings("deprecation")
	public static final byte[] postDealRequest(String chargeUrl)
			throws HttpException, IOException {
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = null;
		byte[] response = null;
		try {
			Protocol https = new Protocol("https",
					new SecureProtocolSocketFactory(), 443);
			Protocol.registerProtocol("https", https);
			postMethod = new PostMethod(chargeUrl);
			postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
					new DefaultHttpMethodRetryHandler(10, false));

			httpClient.getHttpConnectionManager().getParams()
					.setConnectionTimeout(CONNECTION_TIMEOUT);
			httpClient.getParams().setSoTimeout(READ_TIMEOUT);
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode == HttpStatus.SC_OK) {
				response = postMethod.getResponseBody();
			}
		} finally {
			if (postMethod != null) {
				postMethod.releaseConnection();
			}
		}
		return response;
	}

}
